package com.jk.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.HashMap;

public class JwtUtils {
    private static String secretKey = "wwww";
//    @Value("${JWT.secretKey}")
//    public void secretKey(String secretKey) {
//        JwtUtils.secretKey =  secretKey;
//    }
    // 生成jwt
    public static String createJwt(String userId) {
        HashMap headers = new HashMap();
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND, 1800);
        String token = JWT.create().withHeader(headers) //headers
                .withClaim("userId", userId)
                .withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(secretKey));// 密钥
        System.out.println(token);
        return token;

    }
    // 验证jwt 可以获取页眉数据和有效荷载数据
    public static String checkToken(String toke) {
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secretKey)).build();
        DecodedJWT decodedJWT = jwtVerifier.verify(toke);
        String userId = decodedJWT.getClaim("userId").asString();
        System.out.println(userId);
        return userId;
    }
}